package com.crm.system.service.impl.index;

import com.crm.common.model.DhPageInfo;
import com.crm.model.entity.sys.Notify;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.recruitmentManagement.AnnouncementQO;
import com.crm.model.vo.announcement.AnnouncementVO;
import com.crm.service.index.AnnouncementDbService;
import com.crm.system.service.index.AnnouncementService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author wll
 * @date 2020/12/18 14:09
 */
@Service
public class AnnouncementServiceImpl implements AnnouncementService {

    @Autowired
    AnnouncementDbService announcementDbService;


    @Override
    public DhPageInfo<AnnouncementVO> getList(Integer pageIndex, Integer pageSize, String searchInfo, Integer publicStatus,String sort,String sortOrder) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        List<Notify> announcements = announcementDbService.selectAnnouncementBySearchInfo(searchInfo, publicStatus,sort,sortOrder);
        PageInfo<Notify> pageInfo = new PageInfo<>(announcements);
        List<AnnouncementVO> result = new ArrayList<AnnouncementVO>();
        for (Notify dbItem : pageInfo.getList()) {
            result.add(new AnnouncementVO(dbItem));
        }
        return new DhPageInfo<AnnouncementVO>(pageInfo, result);
    }



    /**
     * 添加公告
     */
    @Override
    public Boolean add(AnnouncementQO announcementQO) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        Notify notify = new Notify();
        BeanUtils.copyProperties(announcementQO, notify);
        //处理数据
        Notify notifyData = this.notifyData(notify, UUID.randomUUID().toString().replaceAll("-", ""), userAccount.getUserName(), announcementQO);
        notifyData.setCreateTm(new Date());
        notifyData.setCreatePeople(userAccount.getUserName());
        notifyData.setType(1);
        Boolean result = announcementDbService.insertAnnouncement(notify);
        return result;
    }



    /**
     * 修改公告
     */
    @Override
    public Boolean update(AnnouncementQO announcementQO) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        Notify notify = new Notify();
        //处理数据
        Notify notifyData = this.notifyData(notify, announcementQO.getNotifyId(), userAccount.getUserName(), announcementQO);
        notifyData.setTitle(announcementQO.getTitle());
        notifyData.setIsTop(announcementQO.getIsTop());
        notifyData.setNotifyContent(announcementQO.getNotifyContent());
        Boolean result = announcementDbService.updateAnnouncementByPrimaryKey(notify);
        return result;
    }



    /**
     * 删除公告
     */
    @Override
    public Boolean delete(String id) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        Notify notify = new Notify();
        notify.setNotifyId(id);
        notify.setDeleteStatus(Short.parseShort("1"));
        notify.setUpdateTm(new Date());
        notify.setUpdatePeople(userAccount.getUserName());
        Boolean result = announcementDbService.updateAnnouncementByPrimaryKey(notify);
        return result;
    }



    /**
     * 修改公告置顶状态
     */
    @Override
    public Boolean updateIstop(String id, Integer isTop) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        Notify notify = new Notify();
        notify.setNotifyId(id);
        notify.setIsTop(isTop);
        notify.setUpdateTm(new Date());
        notify.setUpdatePeople(userAccount.getUserName());
        Boolean result = announcementDbService.updateAnnouncementByPrimaryKey(notify);
        return result;
    }

    @Override
    public Notify selectNotifyById(String id) {
        Notify notify = announcementDbService.selectAnnouncementById(id);
        return notify;
    }


    /**
     * info 提取公共数据
     */
    private Notify notifyData(Notify notify, String id, String userName, AnnouncementQO announcementQO) {
        notify.setNotifyId(id);
        notify.setUpdateTm(new Date());
        notify.setUpdatePeople(userName);
        notify.setUploadId(announcementQO.getFileUrls());
        return notify;
    }


}
